<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Member_report extends MX_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model(array('admin_commoncall','admin_settings_model','admin_review_model','admin_news_model','admin_member_model','admin_location_model','admin_counter_model'));
		$this->load->library('pagination');
		//$this->admin_commoncall->check_admin_login();
		$this->template->set_theme('admin');
		$this->template->set_layout('default');
		//$this->template->set_partial('side_menu','layouts/side_menu');
		//$this->template->set_partial('nav_bar','layouts/nav_bar');

	}

	function index()
	{
		//-- Default Value --//
		$status=51;
		$featured=false;
		$search = "";

		if($this->input->post('order')){
			$order = $this->input->post('order');

			switch ($order) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;

			}
		}

		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_all_members('member');

		//print "$total";
		//die();

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		$config['base_url'] = base_url() . 'secure/reports/member_report/index/';
		
		$cur =base_url()."secure/reports/member_report/index/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		

		if($status==51)
		{
			//$data['review']['all'] = $this->admin_review_model->get_reviews($featured = $featured, $status = $status, $perpage, $offset);

			$data['member']['all'] = $this->admin_member_model->get_members($status = $status, $perpage, $offset);


			$this->pagination->initialize($config);
		}else{

			$status=1;
			$offset = 0 + $this->uri->segment(5);
			
			$total = $this->admin_counter_model->count_inactive_members($status);

			//print "$total";
			//die();

			$perpage = 5;
			$config['per_page'] = $perpage;
			$config['total_rows'] = $total;
			$config['uri_segment'] = '5';

			
			$config['base_url'] = base_url() . 'secure/reports/member_report/inactive/';
			
			
			$cur =base_url()."secure/reports/member_report/inactive/";
			$cur = '<a href="'. $cur   .'">&laquo; First</a>';
			$config['full_tag_open'] = '<div class="pagination"><ul>';
			$config['full_tag_close'] = '</ul></div><!--pagination-->';
			$config['first_link'] = '&laquo; First';
			$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['first_tag_close'] = '</li>';
			$config['last_link'] = 'Last &raquo;';
			$config['last_tag_open'] = '<li class="next page" style="display:inline">';
			$config['last_tag_close'] = '</li>';
			$config['next_link'] = 'Next &rarr;';
			$config['next_tag_open'] = '<li class="next page" style="display:inline">';
			$config['next_tag_close'] = '</li>';
			$config['prev_link'] = '&larr; Previous';
			$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open'] = '<li class="active"><a href="">';
			$config['cur_tag_close'] = '</a></li>';
			$config['num_tag_open'] = '<li class="page">';
			$config['num_tag_close'] = '</li>';
			$config['display_pages'] = FALSE;
			
			$data['member']['all'] = $this->admin_member_model->get_inactive_members($status = $status, $perpage, $offset);

			$this->pagination->initialize($config);
		}
		//$data['member']['all'] = $this->admin_member_model->get_members($status = $status);

		if($this->input->post('search')){
			$search = 	trim($this->input->post('search'));
			$data['member']['all'] = $this->admin_member_model->search($search = $search);
			//print $search;
		}


		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('reports/member_report',$data);
	}


	function inactive()
	{

		//-- Default Value --//
		$status=1;
		$featured=false;
		$search = "";

		if($this->input->post('filter')){
			$filter = $this->input->post('filter');

			switch ($filter) {
				case 'active':
					$status = 51;
					break;
				case 'inactive':
					$status = 1;
					break;
				case 'featured':
					$status = 51;
					$featured=true;
					break;
			}
		}

		$status=1;
		$offset = 0 + $this->uri->segment(5);
		
		$total = $this->admin_counter_model->count_inactive_members($status);

		//print "$total";
		//die();

		$perpage = 5;
		$config['per_page'] = $perpage;
		$config['total_rows'] = $total;
		$config['uri_segment'] = '5';

		$config['base_url'] = base_url() . 'secure/reports/member_report/inactive/';
		
		
		$cur =base_url()."secure/reports/member_report/inactive/";
		$cur = '<a href="'. $cur   .'">&laquo; First</a>';
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div><!--pagination-->';
		$config['first_link'] = '&laquo; First';
		$config['first_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['first_tag_close'] = '</li>';
		$config['last_link'] = 'Last &raquo;';
		$config['last_tag_open'] = '<li class="next page" style="display:inline">';
		$config['last_tag_close'] = '</li>';
		$config['next_link'] = 'Next &rarr;';
		$config['next_tag_open'] = '<li class="next page" style="display:inline">';
		$config['next_tag_close'] = '</li>';
		$config['prev_link'] = '&larr; Previous';
		$config['prev_tag_open'] = '<li class="prev page" style="display:inline">';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="">';
		$config['cur_tag_close'] = '</a></li>';
		$config['num_tag_open'] = '<li class="page">';
		$config['num_tag_close'] = '</li>';
		$config['display_pages'] = FALSE;
		
		$data['member']['all'] = $this->admin_member_model->get_inactive_members($status = $status, $perpage, $offset);

		$this->pagination->initialize($config);

		if($this->input->post('search')){
			$search = 	trim($this->input->post('search'));
			$data['member']['all'] = $this->admin_member_model->search($search = $search);
			//print $search;
		}

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('reports/member_report',$data);
	}

	function action()
	{
		$status=NULL;
		$id=NULL;

		if($this->input->post('action')){
			$action = $this->input->post('action');
			$id = $this->input->post('member_id');

			switch ($action) {
				case 'active':
					$status = 51;

					$this->admin_member_model->change_status($status,$id);

					break;
				case 'inactive':
					$status = 1;
					$this->admin_member_model->change_status($status,$id);
					break;
				case 'delete':

					$this->admin_member_model->delete($id);
					break;
				case 'edit':

					$url= 'secure/reports/member_report/edit/'.$id;
					redirect($url);
					break;
			}
		}

		redirect('secure/reports/member_report');
	}

	function add()
	{

		$data['mode'] = "ADD";
		$data['country']= $this->admin_location_model->get_countries(99999);

		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('reports/member_add',$data);
	}

	function edit($id)
	{

		$data['mode'] = "EDIT";
		$data['member']['details'] = $this->admin_member_model->get_member_details($id);

		$data['country']= $this->admin_location_model->get_countries(99999);

		$data['state']= $this->admin_location_model->get_state($data['member']['details']->country);
		$data['city']= $this->admin_location_model->get_city($data['member']['details']->state);



		//var_dump($data['member']['details']);
		//die();

		$data['id'] = $id;
		$this->template->title('Welcome', 'Ypage.com')
		->set_partial('nav_bar','layouts/nav_bar')
		->set_partial('side_menu','layouts/side_menu')
		//->set_partial('footer', 'footer/footer')
		->build('reports/member_add',$data);
	}

	function submit()
	{
		$this->form_validation->set_rules('mem_type', 'Member Type', 'trim|required|xss_clean');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');

		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
		$this->form_validation->set_rules('member_country', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('member_state', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('member_city', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('iagree', 'You must agree', 'trim|required');

		if ($this->form_validation->run() == true) {
			$post_data = array(
					'type' => $this->input->post('mem_type'),
					'title' => $this->input->post('title'),
					'name' => $this->input->post('name'),
					'phone' => $this->input->post('phone'),
					'email' => $this->input->post('email'),
					'password'=>$this->commoncall->random_text(6),
					'country' =>$this->input->post('member_country'),
					'state' => $this->input->post('member_state'),
					'city' => $this->input->post('member_city'),
					'created_on'=> date('Y-m-d H:i:s'),
					'last_login_ip'	=> $this->commoncall->get_ip(),
					'agree'=>$_POST['iagree'],


					//'retype_email' => $this->input->post('retype_email'),
			);

			//var_dump($post_data);
			//die();


			$this->db->insert('member_master',$post_data);
			$msg = "<span class='error'><strong>Thank you Please check your Email and Login!</strong> </span>";
			$this->session->set_flashdata('message', $msg);
			redirect('secure/reports/member_report/add');
			exit();
		}else{
			$this->add();
		}

	}

	function update()
	{


		$this->form_validation->set_rules('mem_type', 'Member Type', 'trim|required|xss_clean');
		$this->form_validation->set_rules('title', 'Title', 'trim|required|xss_clean');

		$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
		$this->form_validation->set_rules('phone', 'Phone', 'trim|required|numeric');
		$this->form_validation->set_rules('member_country', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('member_state', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('member_city', 'Location', 'trim|required|numeric');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
		$this->form_validation->set_rules('iagree', 'You must agree', 'trim|required');

		if ($this->form_validation->run() == true) {
			$id = $this->input->post('id');
			$post_data = array(
					'type' => $this->input->post('mem_type'),
					'title' => $this->input->post('title'),
					'name' => $this->input->post('name'),
					'phone' => $this->input->post('phone'),
					'email' => $this->input->post('email'),
					'password'=>$this->commoncall->random_text(6),
					'country' =>$this->input->post('member_country'),
					'state' => $this->input->post('member_state'),
					'city' => $this->input->post('member_city'),
					'created_on'=> date('Y-m-d H:i:s'),
					'last_login_ip'	=> $this->commoncall->get_ip(),
					'agree'=>$_POST['iagree'],


					//'retype_email' => $this->input->post('retype_email'),
			);

			//var_dump($post_data);
			//die();

			$this->db->where('id',$id);
			$this->db->update('member_master',$post_data);

			$this->session->set_flashdata('message', 'Member Updated Successfully!... ');
			redirect('secure/reports/member_report/edit/'.$this->input->post('id'));

		}else{
			$this->session->set_flashdata('message', 'Invalid!... ');
			redirect('secure/reports/member_report/edit/'.$this->input->post('id'));
		}

	}

	function change_member_status($status=NULL,$id=NULL){
		$this->admin_member_model->change_status($id,$status);
		redirect('secure/reports/member_report');
	}

	function delete_member($id){
		$this->admin_member_model->delete($id);
		redirect('secure/reports/member_report');
	}
}